﻿// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="TestAddIndexedCMFlattenDenseMatrixFactor.cs" company="Exul">
//     Copyright (c) Exul. All rights reserved.
// </copyright>
// <author>
//     Exul
// </author>
// -------------------------------------------------------------------------------------------------------------------------------

namespace ExulLibrary.Mathematics.LinearAlgebra.Sequential.Tests.CcsSparseMatrixTest.DoubleExtensions
{
    #region Usings

    using System;
    using System.Globalization;

    using ExulLibrary.Exceptions;
    using ExulLibrary.Mathematics.LinearAlgebra.Exceptions;
    using ExulLibrary.Mathematics.LinearAlgebra.Sequential.DoubleExtensions;

    using NUnit.Framework;

    #endregion

    /// <summary>
    ///     Test suite for
    ///     <see cref="CcsSparseMatrixExtensions.Add(CcsSparseMatrix{double},int[],int[],CMFlattenDenseMatrix{double},double)"/>.
    /// </summary>
    [TestFixture]
    [Category("ExulLibrary.Mathematics.LinearAlgebra.Sequential.DoubleExtensions.CcsSparseMatrixExtensions")]
    public sealed class TestAddIndexedCMFlattenDenseMatrixFactor
    {
        /// <summary>
        ///     Tests the method when indexes specify existing elements.
        /// </summary>
        [Test]
        public void WhenAugendIndexesAreValid()
        {
            // Arrange
            // Matrix:
            // 1(Y) 3 5(Y)
            // 0    4 0
            // 2(Y) 0 0(Y)
            // 8    0 7
            var augend = new CcsSparseMatrix<double>(
                rowsCount: 4,
                columnsCount: 3,
                columnsBegins: new[] { 0, 3, 5, 7 },
                rowsIndexes: new[] { 0, 2, 3, 0, 1, 0, 3 },
                data: new double[] { 1, 2, 8, 3, 4, 5, 7 });

            var augendRowsIndexes = new[] { 0, 3 };
            var augendColumnsIndexes = new[] { 0, 2 };

            var data = new double[]
                       {
                           10, 20,
                           30, 40
                       };
            var addend = new CMFlattenDenseMatrix<double>(
                rowsCount: 2,
                columnsCount: 2,
                data: data);

            // Act
            augend.Add(augendRowsIndexes, augendColumnsIndexes, addend, 2);

            // Assert
            Assert.That(augend.Values, Is.EquivalentTo(new[] { 21, 2, 48, 3, 4, 65, 87 }));
        }

        /// <summary>
        ///     Tests the method when augendRowsIndexes parameter is empty collection.
        /// </summary>
        [Test]
        public void WhenRowsIndexesCollectionIsEmpty()
        {
            // Arrange
            // Matrix:
            // 1 3 5
            // 0 4 0
            // 2 0 0
            // 8 0 7
            var augend = new CcsSparseMatrix<double>(
                rowsCount: 4,
                columnsCount: 3,
                columnsBegins: new[] { 0, 3, 5, 7 },
                rowsIndexes: new[] { 0, 2, 3, 0, 1, 0, 3 },
                data: new double[] { 1, 2, 8, 3, 4, 5, 7 });

            var augendRowsIndexes = new int[0];
            var augendColumnsIndexes = new[] { 0, 2 };

            var data = new double[]
                       {
                           10, 20,
                           30, 40
                       };
            var addend = new CMFlattenDenseMatrix<double>(
                rowsCount: 2,
                columnsCount: 2,
                data: data);

            // Act
            augend.Add(augendRowsIndexes, augendColumnsIndexes, addend, 2);

            // Assert
            Assert.That(augend.Values, Is.EquivalentTo(new[] { 1, 2, 8, 3, 4, 5, 7 }));
        }

        /// <summary>
        ///     Tests the method when augendColumnsIndexes parameter is empty collection.
        /// </summary>
        [Test]
        public void WhenColumnsIndexesCollectionIsEmpty()
        {
            // Arrange
            // Matrix:
            // 1 3 5
            // 0 4 0
            // 2 0 0
            // 8 0 7
            var augend = new CcsSparseMatrix<double>(
                rowsCount: 4,
                columnsCount: 3,
                columnsBegins: new[] { 0, 3, 5, 7 },
                rowsIndexes: new[] { 0, 2, 3, 0, 1, 0, 3 },
                data: new double[] { 1, 2, 8, 3, 4, 5, 7 });

            var augendRowsIndexes = new[] { 0, 3 };
            var augendColumnsIndexes = new int[0];

            var data = new double[]
                       {
                           10, 20,
                           30, 40
                       };
            var addend = new CMFlattenDenseMatrix<double>(
                rowsCount: 2,
                columnsCount: 2,
                data: data);

            // Act
            augend.Add(augendRowsIndexes, augendColumnsIndexes, addend, 2);

            // Assert
            Assert.That(augend.Values, Is.EquivalentTo(new[] { 1, 2, 8, 3, 4, 5, 7 }));
        }

#if !SAFE

        /// <summary>
        ///     Tests the method when indexes specify an element not in portrait of the augend matrix.
        /// </summary>
        [Test]
        public void WhenAugendIndexesSpecifyNotAnElementInPortrait()
        {
            // Arrange
            // Matrix:
            // 1(Y) 3 5(Y)
            // 0    4 0
            // 2(Y) 0 0(Y)
            // 8    0 7
            var augend = new CcsSparseMatrix<double>(
                rowsCount: 4,
                columnsCount: 3,
                columnsBegins: new[] { 0, 3, 5, 7 },
                rowsIndexes: new[] { 0, 2, 3, 0, 1, 0, 3 },
                data: new double[] { 1, 2, 8, 3, 4, 5, 7 });

            var augendRowsIndexes = new[] { 0, 2 };
            var augendColumnsIndexes = new[] { 0, 2 };

            var data = new double[]
            {
                10, 20,
                30, 40
            };
            var addend = new CMFlattenDenseMatrix<double>(
                rowsCount: 2,
                columnsCount: 2,
                data: data);

            // Act
            augend.Add(augendRowsIndexes, augendColumnsIndexes, addend, 2);

            // Assert
            Assert.That(augend.Values, Is.EquivalentTo(new[] { 21, 42, 8, 3, 4, 65, 7 }));
        }

#endif

#if SAFE

        /// <summary>
        ///     Tests the method when indexes specify an element not in portrait of the augend matrix.
        /// </summary>
        [Test]
        public void WhenAugendIndexesSpecifyNotAnElementInPortrait()
        {
            // Arrange
            // Matrix:
            // 1(Y) 3 5(Y)
            // 0    4 0
            // 2(Y) 0 0(Y)
            // 8    0 7
            var augend = new CcsSparseMatrix<double>(
                rowsCount: 4,
                columnsCount: 3,
                columnsBegins: new[] { 0, 3, 5, 7 },
                rowsIndexes: new[] { 0, 2, 3, 0, 1, 0, 3 },
                data: new double[] { 1, 2, 8, 3, 4, 5, 7 });

            var augendRowsIndexes = new[] { 0, 2 };
            var augendColumnsIndexes = new[] { 0, 2 };

            var data = new double[]
                       {
                           10, 20,
                           30, 40
                       };
            var addend = new CMFlattenDenseMatrix<double>(
                rowsCount: 2,
                columnsCount: 2,
                data: data);

            // Act and Assert
            Assert.That(
                () => augend.Add(augendRowsIndexes, augendColumnsIndexes, addend, 2),
                Throws.TypeOf<ReadOnlyMatrixElementException>().With.Property("ParamName").EqualTo("augend"));
        }

        /// <summary>
        ///     Tests the method when augend parameter is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenAugendIsNull()
        {
            var augendRowsIndexes = new[] { 0, 3 };
            var augendColumnsIndexes = new[] { 0, 2 };

            var data = new double[]
                       {
                           10, 20,
                           30, 40
                       };
            var addend = new CMFlattenDenseMatrix<double>(
                rowsCount: 2,
                columnsCount: 2,
                data: data);

            // Act and Assert
            Assert.That(
                () => CcsSparseMatrixExtensions.Add(null, augendRowsIndexes, augendColumnsIndexes, addend, 2),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("augend"));
        }

        /// <summary>
        ///     Tests the method when addend parameter is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenAddendIsNull()
        {
            // Arrange
            // Matrix:
            // 1 3 5
            // 0 4 0
            // 2 0 0
            // 8 0 7
            var augend = new CcsSparseMatrix<double>(
                rowsCount: 4,
                columnsCount: 3,
                columnsBegins: new[] { 0, 3, 5, 7 },
                rowsIndexes: new[] { 0, 2, 3, 0, 1, 0, 3 },
                data: new double[] { 1, 2, 8, 3, 4, 5, 7 });

            var augendRowsIndexes = new[] { 0, 3 };
            var augendColumnsIndexes = new[] { 0, 2 };

            // Act and Assert
            Assert.That(
                () => augend.Add(augendRowsIndexes, augendColumnsIndexes, (CMFlattenDenseMatrix<double>)null, 2),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("addend"));
        }

        /// <summary>
        ///     Tests the method when augendRowsIndexes parameter is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenAugendRowsIndexesCollectionIsNull()
        {
            // Arrange
            // Matrix:
            // 1 3 5
            // 0 4 0
            // 2 0 0
            // 8 0 7
            var augend = new CcsSparseMatrix<double>(
                rowsCount: 4,
                columnsCount: 3,
                columnsBegins: new[] { 0, 3, 5, 7 },
                rowsIndexes: new[] { 0, 2, 3, 0, 1, 0, 3 },
                data: new double[] { 1, 2, 8, 3, 4, 5, 7 });

            var augendColumnsIndexes = new[] { 0, 2 };

            var data = new double[]
                       {
                           10, 20,
                           30, 40
                       };
            var addend = new CMFlattenDenseMatrix<double>(
                rowsCount: 2,
                columnsCount: 2,
                data: data);

            // Act and Assert
            Assert.That(
                () => augend.Add(null, augendColumnsIndexes, addend, 2),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("augendRowsIndexes"));
        }

        /// <summary>
        ///     Tests the method when augendColumnsIndexes parameter is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenAugendColumnsIndexesCollectionIsNull()
        {
            // Arrange
            // Matrix:
            // 1 3 5
            // 0 4 0
            // 2 0 0
            // 8 0 7
            var augend = new CcsSparseMatrix<double>(
                rowsCount: 4,
                columnsCount: 3,
                columnsBegins: new[] { 0, 3, 5, 7 },
                rowsIndexes: new[] { 0, 2, 3, 0, 1, 0, 3 },
                data: new double[] { 1, 2, 8, 3, 4, 5, 7 });

            var augendRowsIndexes = new[] { 0, 3 };

            var data = new double[]
                       {
                           10, 20,
                           30, 40
                       };
            var addend = new CMFlattenDenseMatrix<double>(
                rowsCount: 2,
                columnsCount: 2,
                data: data);

            // Act and Assert
            Assert.That(
                () => augend.Add(augendRowsIndexes, null, addend, 2),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("augendColumnsIndexes"));
        }

        /// <summary>
        ///     Tests the method when an element of augendRowsIndexes collection is negative or greater than or equal to
        ///     the number of rows of the augend matrix.
        /// </summary>
        /// <param name="rowIndex">
        ///     The row index.
        /// </param>
        /// <param name="location">
        ///     The location of the index in the storage.
        /// </param>
        [Test]
        public void WhenRowIndexIsOutOfRange([Values(-1, 4)] int rowIndex, [Values(0, 1)] int location)
        {
            // Arrange
            // Matrix:
            // 1 3 5
            // 0 4 0
            // 2 0 0
            // 8 0 7
            var augend = new CcsSparseMatrix<double>(
                rowsCount: 4,
                columnsCount: 3,
                columnsBegins: new[] { 0, 3, 5, 7 },
                rowsIndexes: new[] { 0, 2, 3, 0, 1, 0, 3 },
                data: new double[] { 1, 2, 8, 3, 4, 5, 7 });

            var augendRowsIndexes = new int[2];
            augendRowsIndexes[location] = rowIndex;

            var augendColumnsIndexes = new[] { 0, 2 };

            var data = new double[]
                       {
                           10, 20,
                           30, 40
                       };
            var addend = new CMFlattenDenseMatrix<double>(
                rowsCount: 2,
                columnsCount: 2,
                data: data);

            // Act and Assert
            var argumentName = string.Format(CultureInfo.InvariantCulture, "augendRowsIndexes[{0}]", location);
            Assert.That(
                () => augend.Add(augendRowsIndexes, augendColumnsIndexes, addend, 2),
                Throws.TypeOf<ElementIndexIsOutOfRangeException>().With.Property("ParamName").EqualTo(argumentName));
        }

        /// <summary>
        ///     Tests the method when an element of augendColumnsIndexes collection is negative or greater than or equal to
        ///     the number of columns of the augend matrix.
        /// </summary>
        /// <param name="columnIndex">
        ///     The column index.
        /// </param>
        /// <param name="location">
        ///     The location of the index in the storage.
        /// </param>
        [Test]
        public void WhenColumnIndexIsOutOfRange([Values(-1, 3)] int columnIndex, [Values(0, 1)] int location)
        {
            // Arrange
            // Matrix:
            // 1 3 5
            // 0 4 0
            // 2 0 0
            // 8 0 7
            var augend = new CcsSparseMatrix<double>(
                rowsCount: 4,
                columnsCount: 3,
                columnsBegins: new[] { 0, 3, 5, 7 },
                rowsIndexes: new[] { 0, 2, 3, 0, 1, 0, 3 },
                data: new double[] { 1, 2, 8, 3, 4, 5, 7 });

            var augendRowsIndexes = new[] { 0, 3 };
            var augendColumnsIndexes = new int[2];
            augendColumnsIndexes[location] = columnIndex;

            var data = new double[]
                       {
                           10, 20,
                           30, 40
                       };
            var addend = new CMFlattenDenseMatrix<double>(
                rowsCount: 2,
                columnsCount: 2,
                data: data);

            // Act and Assert
            var argumentName = string.Format(CultureInfo.InvariantCulture, "augendColumnsIndexes[{0}]", location);
            Assert.That(
                () => augend.Add(augendRowsIndexes, augendColumnsIndexes, addend, 2),
                Throws.TypeOf<ElementIndexIsOutOfRangeException>().With.Property("ParamName").EqualTo(argumentName));
        }

#endif
    }
}